package com.cuit.zuke.product.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.zuke.product.mapper.StockMapper;
import com.cuit.zuke.product.model.vo.StockExVO;
import com.cuit.zuke.product.model.vo.StockVO;
import com.cuit.zuke.product.model.entity.Stock;
import com.cuit.zuke.product.model.vo.StockQueryVO;
import com.cuit.zuke.product.service.GoodsService;
import com.cuit.zuke.product.service.StockService;
import com.cuit.zuke.product.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private WarehouseService warehouseService;


    /**
     * 分页查询带条件
     * @param current
     * @param limit
     * @return
     */
    public Page<StockVO> findPage(Integer current, Integer limit, StockQueryVO stockQueryVO){
        Page<Stock> stockPage = new Page<>(current, limit);
        Page<StockVO> stockDTOPage = new Page<>(stockPage.getCurrent(), stockPage.getSize());

        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
        Long goodsId = stockQueryVO.getGoodsId();
        Integer warehouseId = stockQueryVO.getWarehouseId();
        Long stockId = stockQueryVO.getStockId();
        if(!StringUtils.isEmpty(goodsId)){
            queryWrapper.eq("goods_id",goodsId);
        }
        if(!StringUtils.isEmpty(warehouseId)){
            queryWrapper.eq("warehouse_id",warehouseId);
        }
        if(!StringUtils.isEmpty(stockId)){
            queryWrapper.eq("id",stockId);
        }

        stockPage = baseMapper.selectPage(stockPage, queryWrapper);
        List<Stock> stockPageRecords = stockPage.getRecords();

        List<StockVO> stockVOPageRecords = new ArrayList<>();
        for (Stock stock : stockPageRecords) {
            StockVO stockVO = new StockVO();
            String goodsName = goodsService.getGoodsName(stock.getGoodsId());
            String warehouseName = warehouseService.getNameById(stock.getWarehouseId());
            String fullLocName = warehouseService.getFullLocNameById(stock.getWarehouseId());
            stockVO.setSupplier(stock.getSupplier());
            stockVO.setId(stock.getId());
            stockVO.setNum(stock.getNum());
            stockVO.setGoodsName(goodsName);
            stockVO.setPrice(stock.getPrice());
            stockVO.setTel(stock.getTel());
            stockVO.setCreat_time(stock.getCreateTime());
            stockVO.setWarehouseName(warehouseName);
            stockVO.setWarehouseLoc(fullLocName);
            stockVOPageRecords.add(stockVO);
        }
        stockDTOPage.setRecords(stockVOPageRecords);
        stockDTOPage.setTotal(stockPage.getTotal());
        return stockDTOPage;
    }

    @Override
    public boolean updateStock(Stock stock) {
        stock.setUpdateTime(new Date());
        QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",stock.getId());
        int i = baseMapper.update(stock, queryWrapper);
        return i > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        int i = baseMapper.deleteById(id);
        return i > 0;
    }

    @Override
    public boolean addStock(Stock stock) {
        // 判断仓库中是否有相同货物
        QueryWrapper<Stock> stockQueryWrapper = new QueryWrapper<>();
        stockQueryWrapper.eq("warehouse_id",stock.getWarehouseId());
        stockQueryWrapper.eq("goods_id",stock.getWarehouseId());
        stockQueryWrapper.eq("supplier",stock.getSupplier());
        Stock stock1 = baseMapper.selectOne(stockQueryWrapper);
        int i = 0;
        // 仓库中有
        if (stock1 != null){
            stock1.setTel(stock.getTel());
            stock1.setNum(stock1.getNum()+stock.getNum());
            stock1.setPrice(stock.getPrice());
            stock1.setUpdateTime(new Date());
            QueryWrapper<Stock> wrapper = new QueryWrapper<>();
            wrapper.eq("id",stock1.getId());
            i = baseMapper.update(stock1, wrapper);
        }else{
            i = baseMapper.insert(stock);
        }

        return i > 0;
    }

    /**
     * 以excel格式导出
     * @param response
     */
    @Override
    public void exportStockData(HttpServletResponse response) {
        // 设置以下载方式打开
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String filename = "stock";
        response.setHeader("Content-disposition","attachment;filename="+filename+".xlsx");

        // 查询数据库
        List<Stock> stockList = baseMapper.selectList(null);

        // stock -> stockExVO
        List<StockExVO> stockExVOS = new ArrayList<>(stockList.size());
        for (Stock stock : stockList) {
            StockExVO stockExVO = new StockExVO();
            // 仓库id
            stockExVO.setId(stock.getId());
            // 商品名
            String goodsName = goodsService.getGoodsName(stock.getGoodsId());
            stockExVO.setGoodsName(goodsName);
            // 仓库名
            String warehouseName = warehouseService.getNameById(stock.getWarehouseId());
            stockExVO.setWarehouseName(warehouseName);
            // 仓库地址
            String fullLoc = warehouseService.getFullLocNameById(stock.getWarehouseId());
            stockExVO.setWarehouseLoc(fullLoc);
            // 库存数量
            stockExVO.setNum(stock.getNum());
            // 进价
            stockExVO.setPrice(stock.getPrice());
            // 联系人电话
            stockExVO.setTel(stock.getTel());
            // 入库时间
            stockExVO.setCreat_time(stock.getCreateTime());
            stockExVOS.add(stockExVO);
        }
        // 写excel
        try {
            EasyExcel.write(response.getOutputStream(), StockExVO.class)
                    .sheet("goods").doWrite(stockExVOS);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
